package com.github.postsanf.yinian.activity;

import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class ImageLocationMapActivity extends BaseActivity implements View.OnClickListener, LocationSource, AMapLocationListener {
    //    public AMapLocationClient mLocationClient = null;
//    public AMapLocationClientOption mLocationOption = null;
    //    private PoiSearch.Query query;
//    private PoiResult poiResult;
//    private ProgressDialog mProgressDialog;
    private boolean isBottom;
    private MapView mMapView;
    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_location_editmap);
        initView(savedInstanceState);
    }

    OnLocationChangedListener mListener;
    AMapLocationClient mlocationClient;
    AMapLocationClientOption mlocationOption;

    //    /**
//     * 激活定位
//     */
    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);
            mlocationOption = new AMapLocationClientOption();
            //设置定位回调监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mlocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationOption.setOnceLocation(true);
            mlocationClient.setLocationOption(mlocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mlocationClient.startLocation();//启动定位
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }

    Marker marker;

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (mListener != null && amapLocation != null) {
            if (amapLocation != null
                    && amapLocation.getErrorCode() == 0) {
                mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
                Log.i("xjs", "为纬度===>" + amapLocation.getLongitude());
                Log.i("xjs", "经度===>" + amapLocation.getLatitude());
                Log.i("xjs", "地址===>" + amapLocation.getAddress());
//                MarkerOptions markerOptions = new MarkerOptions();
//                //添加一个位置--经度，维度---marker对应一个markerOptions，用来设置marker的属性等
//                markerOptions.position(new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude()));
//                //添加图标
//                markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_edit_mic));
//                //添加marker
//                marker = aMap.addMarker(markerOptions);
//                marker.setPositionByPixels(400, 400);
            } else {
                String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }

    private TextView tv;

    private void initView(Bundle savedInstanceState) {
        tv = (TextView) findViewById(R.id.tv);
        mMapView = (MapView) findViewById(R.id.location_mapview);
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        aMap.setLocationSource(this);
        aMap.setMyLocationEnabled(true);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(16));
        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                Point point = new Point(400, 400);
                LatLng latLng = aMap.getProjection().fromScreenLocation(point);
                double latitude = latLng.latitude;
                double longitude = latLng.longitude;
                Log.i("xjs", "经经经经经经" + latitude);
                Log.i("xjs", "伟伟伟伟伟伟" + longitude);
                Geocoder gc = new Geocoder(ImageLocationMapActivity.this, Locale.getDefault());
                List<Address> locationList = null;
                try {
                    locationList = gc.getFromLocation(latitude, longitude, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Address address = locationList.get(0);
                tv.setText(address.toString());
            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        mMapView.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        mMapView.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        if (null != mlocationClient) {
            mlocationClient.onDestroy();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }
}
